package com.transino.bop.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XmlWritter {

	private static FileWriter xmlFile;
	private static FileWriter logFile;
	private static String filePath;
	 
	private static void createDataXML(String currentfile, List<Map<String,Object>> result) {
		try {
			logFile = new FileWriter(filePath + "\\log.txt", true);
			Calendar now = Calendar.getInstance();
			xmlFile = new FileWriter(filePath + "\\BOP" + now.getTimeInMillis() + ".xml",false);
			xmlFile.write("<MSG>\n");
			xmlFile.write("<APPTYPE>BOP</APPTYPE>\n");
			xmlFile.write("<CURRENTFILE>" + currentfile + "</CURRENTFILE>\n");
			xmlFile.write("<INOUT>OUT</INOUT>\n");
			xmlFile.write("<TOTALRECORDS>" + result.size() + "</TOTALRECORDS>\n");
			xmlFile.write("<RECORDS>\n");
			xmlFile.write("<REC>\n");
			for(Map<String,Object> map:result) {
				for(String key:map.keySet()) {
					xmlFile.write("<"+key.toUpperCase()+">"+map.get(key)+"</"+key.toUpperCase()+">\n");
				}
			}
			xmlFile.write("</REC>\n");
			xmlFile.write("</RECORDS>\n");
		    xmlFile.write("</MSG>\n");
		    System.out.println("XML�ļ�������");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				xmlFile.close();
				logFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	 }
	
	public static void main(String[] args) {
		filePath = "D:\\gogo";
		List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("ID", "1234567890");
		map.put("name", "WANGWU");
		result.add(map);
		createDataXML("A", result);
	}
}
